package cn.mln521.test;

import cn.mln521.ioc.demo3.Man;
import cn.mln521.ioc.demo3.Person;
import cn.mln521.ioc.demo3.Student;
import cn.mln521.ioc.demo4.UserDao;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * bean作用域测试
 */
public class SpringDemo4 {

    /**
     * Bean的作用域
     */
    @Test
    public void demo() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Person p1 = (Person) context.getBean("person");
        Person p2 = (Person) context.getBean("person");

        System.out.println(p1);
        System.out.println(p2);
    }

    /**
     * bean的生命周期
     */
    @Test
    public void demo1(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Man man = (Man) context.getBean("man");

        System.out.println(man);

        context.close();
    }

    /**
     * bean的完整的生命周期
     */
    @Test
    public void demo2(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Student student = (Student) context.getBean("stu");

        student.run();

        context.close();
    }

    /**
     * add方法的增强
     */
    @Test
    public void demo3() {
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        UserDao userDao = (UserDao) context.getBean("userDao");

        userDao.findAll();
        userDao.add();
        userDao.update();
        userDao.delete();

    }

}
